﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;
using System.Web.Optimization;
using WinnerCMS.BLL;
using WinnerCMS.Common;
using WinnerCMS.Model;

namespace WinnerCMS.WebFactory.Bundles
{
    public class Bundle
    {
        public void Builder(BundleCollection bundles)
        {

            var templates = TemplatesManage.GetUseing;
            if (templates == null)
                return;

            FileInfo fi =
                new FileInfo(HttpServer.MapPath(string.Format("~/Templates/{0}/Bundle.config", templates.TemplatePath)));
            if (!fi.Exists)
            {
                return;
            }
            var config = SerializableHelper.DeSerialize<BundleConfig>(fi);
            if (config == null)
            {
                return;
            }

            Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
            config.StyleBundle.ForEach((x) =>
            {
                var v = new List<string>();
                x.Files.ForEach(y =>
                {
                    var filename = y.StartsWith("~")
                        ? y
                        : string.Format("~/Templates/{0}/{1}", templates.TemplatePath, y);
                    if (filename.Contains("{version}"))
                    {
                        v.Add(filename); return;
                    }
                    if (File.Exists(HttpServer.MapPath(filename)))
                    {
                        v.Add(filename);
                    }
                });
                if (v.Any())
                    dic.Add(x.VirtualPath, v);
            });
            dic.ForEach(x =>
            {
                var sb = new StyleBundle(x.Key);
                x.Value.ForEach(y => sb.Include(y, new CssRewriteUrlTransform()));
                bundles.Add(sb);
            });
            dic.Clear();

            config.ScriptBundle.ForEach((x) =>
            {
                var v = new List<string>();
                x.Files.ForEach(y =>
                {
                    var filename = y.StartsWith("~")
                        ? y
                        : string.Format("~/Templates/{0}/{1}", templates.TemplatePath, y);
                    if (filename.Contains("{version}"))
                    {
                        v.Add(filename); return;
                    }
                    if (File.Exists(HttpServer.MapPath(filename)))
                    {
                        v.Add(filename);
                    }
                });
                if (v.Any())
                    dic.Add(x.VirtualPath, v);
            });
            dic.ForEach(x =>
                bundles.Add(new ScriptBundle(x.Key).Include(x.Value.ToArray()))
                );
        }
    }

    public class ScriptsOrderer : IBundleOrderer
    {

        public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
        {
            files.ForEach(x =>
            {
                x.Transforms.Clear();
                x.Transforms.Add(new EasyUIBundleTransform());
            });
            return files;
        }
    }
}